package com.smarkparking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smarkparking.dao.ParkingDao;
import com.smarkparking.pojo.Parking;
import com.smarkparking.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘涛
 * @create 2021/11/18 9:33
 */
@Service
public class ParkingServiceImpl implements ParkingService {
    @Autowired
    private ParkingDao parkingDao;

    @Override
    public List<String> findParkingAddress() {
        List<Parking> address = parkingDao.findAddress();
        ArrayList<String> list = new ArrayList<>();
        for (Parking parking : address) {
            list.add(parking.getAddress());
        }
        return list;
    }

    @Override
    public List<Parking> findParkingByAddress(String address) {
        QueryWrapper<Parking> pqw = new QueryWrapper<>();
        pqw.eq("address", address);
        List<Parking> parkings = parkingDao.selectList(pqw);
        return parkings;
    }

    @Override
    public void updateParkingStateClose(Integer id) {
        Parking parking = new Parking();
        parking.setState(1);
        parking.setId(id);
        parkingDao.updateById(parking);
    }

    @Override
    public void updateParkingStateOpen(Integer id) {
        Parking parking = new Parking();
        parking.setState(0);
        parking.setId(id);
        parkingDao.updateById(parking);
    }

    @Override
    public Parking selectById(Integer id) {
        Parking parking = parkingDao.selectById(id);
        return parking;
    }

    @Override
    public Parking selectByParkingNumber(String parkingSpaceNumber) {
        QueryWrapper<Parking> pqw = new QueryWrapper<>();
        pqw.eq("parking_space_number", parkingSpaceNumber);
        Parking parking = parkingDao.selectOne(pqw);
        return parking;
    }

    @Override
    public void deleteParking(Integer id) {
        parkingDao.deleteById(id);
    }

    @Override
    public boolean updateParking(Integer id, String address, String parkingSpaceNumber, Double price, Integer state) {
        try {
            Parking parking = new Parking();
            parking.setId(id);
            parking.setState(state);
            parking.setParkingSpaceNumber(parkingSpaceNumber);
            parking.setAddress(address);
            parking.setPrice(price);
            int i = parkingDao.updateById(parking);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean addParking(String address, String parkingSpaceNumber, Double price, Integer state) {
        try {
            Parking parking = new Parking();
            parking.setState(state);
            parking.setParkingSpaceNumber(parkingSpaceNumber);
            parking.setAddress(address);
            parking.setPrice(price);
            int i = parkingDao.insert(parking);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public List<Parking> findAll() {
        return parkingDao.selectList(null);
    }
}
